import hashlib
import json

import pymongo
import redis


class Connect:
    def __init__(self):
        self.mongo_client = pymongo.MongoClient(host='175.178.162.117', port=27017, username="root",
                                                password="H5rOx4dG")
        self.collection = self.mongo_client['py_spider']['']
        self.redis_client = redis.Redis(host='192.168.80.3', port=6379, db=1)

    def save(self, item):
        # redis去重，不存在才入库
        hash_value = hashlib.md5(json.dumps(item, ensure_ascii=False).encode()).hexdigest()
        if self.redis_client.sadd(":filter", hash_value) != 0:
            self.collection.insert_one(item)
            print("入库成功：", item)
        else:
            print("数据重复：")

    def __del__(self):
        self.mongo_client.close()
        self.redis_client.close()
